﻿'
' DKP for DotNetNuke® - http://dkpdnn.codeplex.com
' Copyright (c) 2008
' by Casey Lengacher ( http://dkpdnn.codeplex.com )
'
' DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'

Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.tss_DKP

    Partial Public Class tss_ucRaids

        Inherits Entities.Modules.PortalModuleBase

#Region "Private Variables"

        Private Enum PageState As Integer
            NotSet = -1
            View = 0
            Add = 1
            SingleUpdate = 2
            MultiUpdate = 3
            Import = 4
        End Enum

        Private Enum ImportType As Integer
            Preview = 0
            Confirm = 1
        End Enum

#End Region

#Region "Private Properties"

        Private Property vsPageSize() As Integer
            Get
                If ViewState("tss_RaidPageSize") Is Nothing Then
                    If (Request.Cookies.Get("tss_RaidPageSize") Is Nothing) Then
                        Return ddlPageSize.Items(0).Value
                    Else
                        Return CInt(Request.Cookies.Get("tss_RaidPageSize").Value)
                    End If
                Else
                    Return CInt(ViewState("tss_RaidPageSize"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_RaidPageSize") = Value
                Response.Cookies("tss_RaidPageSize").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_RaidPageSize").Value = Value
            End Set
        End Property

        Private Property vsSortExpression() As String
            Get
                If ViewState("tss_RaidSortExpression") Is Nothing Then
                    If (Request.Cookies.Get("tss_RaidSortExpression") Is Nothing) Then
                        Return "RaidDate"
                    Else
                        Return CStr(Request.Cookies.Get("tss_RaidSortExpression").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_RaidSortExpression"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_RaidSortExpression") = Value
                Response.Cookies("tss_RaidSortExpression").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_RaidSortExpression").Value = Value
            End Set
        End Property

        Private Property vsSortDirection() As String
            Get
                If ViewState("tss_RaidSortDirection") Is Nothing Then
                    If (Request.Cookies.Get("tss_RaidSortDirection") Is Nothing) Then
                        Return "DESC"
                    Else
                        Return CStr(Request.Cookies.Get("tss_RaidSortDirection").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_RaidSortDirection"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_RaidSortDirection") = Value
                Response.Cookies("tss_RaidSortDirection").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_RaidSortDirection").Value = Value
            End Set
        End Property

        Private Property vsCurrentPage() As Integer
            Get
                If ViewState("tss_RaidCurrentPage") Is Nothing Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_RaidCurrentPage"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_RaidCurrentPage") = Value
            End Set
        End Property

        Private Property vsTotalPages() As Integer
            Get
                If ViewState("tss_RaidTotalPages") Is Nothing Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_RaidTotalPages"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_RaidTotalPages") = Value
            End Set
        End Property

        Private Property vsPageState() As PageState
            Get
                If ViewState("tss_RaidPageState") Is Nothing Then
                    Return PageState.NotSet
                Else
                    Return CStr(ViewState("tss_RaidPageState"))
                End If
            End Get
            Set(ByVal Value As PageState)
                ViewState("tss_RaidPageState") = Value
            End Set
        End Property

#End Region

#Region "Private Functions"

        Private Sub BindPage()

            Dim tss_DKP As New tss_DataController
            Dim intRowsAffected As Integer
            Dim intPageNumber As Integer = vsCurrentPage
            Dim dblPagesAffected As Double = 1

            ddlPageSize.SelectedValue = ddlPageSize.Items.FindByText(vsPageSize).Value

            grdRaids.PageSize = vsPageSize

            For Each currentColumn As DataGridColumn In grdRaids.Columns
                currentColumn.HeaderText = currentColumn.HeaderText.Substring(0, _
                    IIf(currentColumn.HeaderText.IndexOf(" <img") > 0, currentColumn.HeaderText.IndexOf(" <img"), currentColumn.HeaderText.Length))
                If (currentColumn.SortExpression = vsSortExpression) Then
                    If (vsSortDirection = "ASC") Then
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_uparrow.png' border=0 height=10 width=10>"
                    Else
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_downarrow.png' border=0 height=10 width=10>"
                    End If
                End If
            Next

            grdRaids.DataSource = tss_DKP.tss_GetRaidsByPage(CInt(Settings("BoardID")), intPageNumber, grdRaids.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
            grdRaids.DataBind()

            If (grdRaids.Items.Count = 0) And (intPageNumber > 1) Then
                intPageNumber = intPageNumber - 1
                vsCurrentPage = intPageNumber.ToString()
                grdRaids.DataSource = tss_DKP.tss_GetRaidsByPage(CInt(Settings("BoardID")), intPageNumber, grdRaids.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
                grdRaids.DataBind()
            End If

            dblPagesAffected = System.Math.Ceiling(intRowsAffected / grdRaids.PageSize)
            If (dblPagesAffected < 1) Then
                dblPagesAffected = 1
            End If
            vsTotalPages = dblPagesAffected.ToString()
            lblCurrentPage.Text = vsCurrentPage
            lblTotalPages.Text = vsTotalPages

            If (intPageNumber = 1) Then
                lnkPreviousPage.Enabled = False

                If (dblPagesAffected > 1) Then
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                Else
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                End If
            Else
                lnkPreviousPage.Enabled = True

                If (intPageNumber = dblPagesAffected) Then
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                Else
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                End If
            End If

            If (DotNetNuke.Security.PortalSecurity.IsInRoles(Me.ModuleConfiguration.AuthorizedEditRoles)) And Not (UserId = -1) Then
                pnlEditPanel.Visible = True
                grdRaids.Columns(0).Visible = True
            Else
                pnlEditPanel.Visible = False
                grdRaids.Columns(0).Visible = False
            End If

        End Sub

        Private Sub ManagePageState(ByVal PageState As PageState)

            Dim tss_DKP As New tss_DataController

            vsPageState = PageState

            If (PageState = PageState.View) Then
                grdRaids.Enabled = True
                BindPage()
            Else
                grdRaids.Enabled = False
            End If

            If (PageState = PageState.View) Then
                pnlEditPanelForm.Visible = False
            Else
                ddlMobID.AutoPostBack = True
                ddlMobID.DataTextField = "MobName"
                ddlMobID.DataValueField = "MobID"
                ddlMobID.DataSource = tss_DKP.tss_GetMobs(CInt(Settings("BoardID")))
                ddlMobID.DataBind()

                pnlEditPanelForm.Visible = True
            End If

            ddlMobID.ClearSelection()
            txtRaidValue.Text = Null.NullString
            txtRaidDate.Text = Null.NullString
            txtRaidRoster.Text = Null.NullString
            txtRaidImport.Text = Null.NullString

            lblRaidConfirmWarning.Text = Null.NullString
            lblWarning.Text = Null.NullString

            If (PageState = PageState.Add) Then

                pnlMobID.Visible = True
                pnlRaidValue.Visible = True
                pnlRaidDate.Visible = True
                pnlRaidRoster.Visible = True
                pnlRaidAttendanceFlag.Visible = True
                pnlRaidImport.Visible = False
                pnlRaidConfirm.Visible = False

                chkIncludeMobID.Visible = False
                chkIncludeRaidValue.Visible = False
                chkIncludeRaidDate.Visible = False
                chkIncludeRaidRoster.Visible = False
                chkIncludeRaidAttendanceFlag.Visible = False

                If (ddlMobID.Items.Count > 0) Then
                    ddlMobID.Items(0).Selected = True
                    BindTextBoxes()
                End If

                ddlMobID.Focus()

            ElseIf (PageState = PageState.SingleUpdate) Then

                pnlMobID.Visible = True
                pnlRaidValue.Visible = True
                pnlRaidDate.Visible = True
                pnlRaidRoster.Visible = False
                pnlRaidAttendanceFlag.Visible = True
                pnlRaidImport.Visible = False
                pnlRaidConfirm.Visible = False

                chkIncludeMobID.Visible = False
                chkIncludeRaidValue.Visible = False
                chkIncludeRaidDate.Visible = False
                chkIncludeRaidRoster.Visible = False
                chkIncludeRaidAttendanceFlag.Visible = False

                ddlMobID.Focus()

            ElseIf (PageState = PageState.MultiUpdate) Then

                pnlMobID.Visible = False
                pnlRaidValue.Visible = True
                pnlRaidDate.Visible = True
                pnlRaidRoster.Visible = False
                pnlRaidAttendanceFlag.Visible = True
                pnlRaidImport.Visible = False
                pnlRaidConfirm.Visible = False

                chkIncludeMobID.Visible = False
                chkIncludeRaidValue.Visible = True
                chkIncludeRaidDate.Visible = True
                chkIncludeRaidRoster.Visible = False
                chkIncludeRaidAttendanceFlag.Visible = True

                chkIncludeMobID.Checked = False
                chkIncludeRaidValue.Checked = False
                chkIncludeRaidDate.Checked = False
                chkIncludeRaidRoster.Checked = False
                chkIncludeRaidAttendanceFlag.Checked = False

                txtRaidValue.Focus()

            ElseIf (PageState = PageState.Import) Then

                pnlMobID.Visible = False
                pnlRaidValue.Visible = False
                pnlRaidDate.Visible = False
                pnlRaidRoster.Visible = False
                pnlRaidAttendanceFlag.Visible = False
                pnlRaidImport.Visible = True
                pnlRaidConfirm.Visible = False

                chkIncludeMobID.Visible = False
                chkIncludeRaidValue.Visible = False
                chkIncludeRaidDate.Visible = False
                chkIncludeRaidRoster.Visible = False
                chkIncludeRaidAttendanceFlag.Visible = False

                txtRaidImport.Focus()

            End If

        End Sub

        Private Sub BindTextBoxes()

            Dim tss_DKP As New tss_DataController
            Dim objMob As tss_MobInfo = tss_DKP.tss_GetMob(ddlMobID.SelectedValue)

            If (objMob Is Nothing) Then
                txtRaidValue.Text = "0"
                chkRaidAttendanceFlag.Checked = True
            Else
                txtRaidValue.Text = objMob.MobValue
                chkRaidAttendanceFlag.Checked = objMob.MobAttendanceFlag
            End If

        End Sub

        Private Function RaidImport(ByVal strRaidImport As String, ByVal ImportType As ImportType) As String

            Dim tss_DKP As New tss_DataController
            Dim dicInstances, dicPlayers, dicMobs, dicRaids, dicItems, dicDrops As New Dictionary(Of String, Integer)
            Dim strWarning As String = Null.NullString

            Dim aryRaidImport() As String = strRaidImport.Replace(vbLf, "").Split(vbCr)

            For intCounter = 0 To (aryRaidImport.Length - 1)

                If (aryRaidImport(intCounter) = "Session:") Then

                    dicInstances.Clear()
                    dicPlayers.Clear()
                    dicMobs.Clear()
                    dicRaids.Clear()
                    dicItems.Clear()
                    dicDrops.Clear()

                    intCounter = intCounter + 2

                    Do Until (aryRaidImport(intCounter) = "") Or (intCounter > (aryRaidImport.Length - 1))

                        Dim arySession() As String = aryRaidImport(intCounter).Split(",")
                        ' Session: Zone,Difficulty,Start,End,Note
                        Dim strInstanceName As String = arySession(0).Trim("""")
                        Dim strDifficulty As String = arySession(1).Trim("""")

                        Dim objExistingInstance As tss_InstanceInfo = tss_DKP.tss_GetInstanceByName(strInstanceName, CInt(Settings("BoardID")))

                        If (objExistingInstance Is Nothing) Then
                            If (ImportType = ImportType.Preview) Then
                                strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                    + "Instance (" + strInstanceName + ") will be added."
                            ElseIf (ImportType = ImportType.Confirm) Then
                                Dim objNewInstance As New tss_InstanceInfo : objNewInstance = CBO.InitializeObject(objNewInstance, GetType(tss_InstanceInfo))
                                objNewInstance.InstanceName = strInstanceName
                                objNewInstance.InstanceWowHeadID = ""
                                objNewInstance.InstanceHeroicFlag = IIf(strDifficulty = "Heroic", True, False)
                                objNewInstance.BoardID = CInt(Settings("BoardID"))
                                Try
                                    dicInstances.Add(strInstanceName, tss_DKP.tss_AddInstance(objNewInstance))
                                Catch ex As Exception
                                    Return "Error: Trying to add instance resulted in the following error (" + ex.Message + ")."
                                End Try
                            End If
                        Else
                            dicInstances.Add(strInstanceName, objExistingInstance.InstanceID)
                            If (strDifficulty = "Heroic") And Not (objExistingInstance.InstanceHeroicFlag) Then
                                If (ImportType = ImportType.Preview) Then
                                    strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                            + "Instance (" + strInstanceName + ") will be updated to Heroic."
                                ElseIf (ImportType = ImportType.Confirm) Then
                                    objExistingInstance.InstanceHeroicFlag = True
                                    Try
                                        tss_DKP.tss_UpdateInstance(objExistingInstance)
                                    Catch ex As Exception
                                        Return "Error: Trying to update instance resulted in the following error (" + ex.Message + ")."
                                    End Try
                                End If
                            End If
                        End If

                        intCounter = intCounter + 1

                    Loop

                ElseIf (aryRaidImport(intCounter) = "Players:") Then

                    intCounter = intCounter + 2

                    Do Until (aryRaidImport(intCounter) = "") Or (intCounter > (aryRaidImport.Length - 1))

                        Dim aryPlayer() As String = aryRaidImport(intCounter).Split(",")
                        ' Player: Index,Name,Join,Leave,Race,Class,Level,Guild,Note
                        Dim strPlayerName As String = aryPlayer(1).Trim("""")
                        Dim strJoinDate As String = aryPlayer(2).Trim("""")
                        Dim strPlayerRace As String = aryPlayer(4).Trim("""")
                        Dim strPlayerClass As String = aryPlayer(5).Trim("""")
                        Dim strPlayerLevel As String = aryPlayer(6).Trim("""")
                        Dim strGuild As String = aryPlayer(7).Trim("""")

                        If (strPlayerRace = "NightElf") Then
                            strPlayerRace = "Night Elf"
                        ElseIf (strPlayerRace = "BloodElf") Then
                            strPlayerRace = "Blood Elf"
                        End If

                        If (strPlayerClass = "DEATHKNIGHT") Then
                            strPlayerClass = "Death Knight"
                        ElseIf (strPlayerClass = "DRUID") Then
                            strPlayerClass = "Druid"
                        ElseIf (strPlayerClass = "HUNTER") Then
                            strPlayerClass = "Hunter"
                        ElseIf (strPlayerClass = "MAGE") Then
                            strPlayerClass = "Mage"
                        ElseIf (strPlayerClass = "PALADIN") Then
                            strPlayerClass = "Paladin"
                        ElseIf (strPlayerClass = "PRIEST") Then
                            strPlayerClass = "Priest"
                        ElseIf (strPlayerClass = "ROGUE") Then
                            strPlayerClass = "Rogue"
                        ElseIf (strPlayerClass = "SHAMAN") Then
                            strPlayerClass = "Shaman"
                        ElseIf (strPlayerClass = "WARLOCK") Then
                            strPlayerClass = "Warlock"
                        ElseIf (strPlayerClass = "WARRIOR") Then
                            strPlayerClass = "Warrior"
                        End If

                        Dim objExistingPlayer As tss_PlayerInfo = tss_DKP.tss_GetPlayerByName(strPlayerName, CInt(Settings("BoardID")))

                        If (objExistingPlayer Is Nothing) Then
                            If (ImportType = ImportType.Preview) Then
                                If (strGuild = CStr(Settings("Guild"))) Then
                                    strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                        + "Player (" + strPlayerName + ") will be added."
                                    dicPlayers.Add(strPlayerName, -1)
                                End If
                            ElseIf (ImportType = ImportType.Confirm) Then
                                If (strGuild = CStr(Settings("Guild"))) Then
                                    Dim objNewPlayer As New tss_PlayerInfo : objNewPlayer = CBO.InitializeObject(objNewPlayer, GetType(tss_PlayerInfo))
                                    objNewPlayer.PlayerName = strPlayerName
                                    objNewPlayer.PlayerRace = strPlayerRace
                                    objNewPlayer.PlayerClass = strPlayerClass
                                    objNewPlayer.PlayerLevel = strPlayerLevel
                                    objNewPlayer.JoinDate = strJoinDate
                                    Try
                                        dicPlayers.Add(strPlayerName, tss_DKP.tss_AddPlayer(objNewPlayer))
                                    Catch ex As Exception
                                        Return "Error: Trying to add player resulted in the following error (" + ex.Message + ")."
                                    End Try
                                End If
                            End If
                        Else
                            dicPlayers.Add(strPlayerName, objExistingPlayer.PlayerID)
                        End If

                        intCounter = intCounter + 1

                    Loop

                ElseIf (aryRaidImport(intCounter) = "Events:") Then

                    intCounter = intCounter + 2

                    Do Until (aryRaidImport(intCounter) = "") Or (intCounter > (aryRaidImport.Length - 1))

                        Dim aryEvent() As String = aryRaidImport(intCounter).Split(",")
                        ' Event: Index,Name,Zone,Difficulty,End,Attendees
                        Dim strInstanceName As String = aryEvent(2).Trim("""")
                        Dim strRaidDate As String = aryEvent(4).Trim("""")
                        Dim aryRoster() As String = aryEvent(5).Trim("""").Split(";")
                        Dim strMobName As String = aryEvent(1).Trim("""")
                        Dim strDifficulty As String = aryEvent(3).Trim("""")
                        Dim strDKPMobName As String = IIf(strDifficulty = "Heroic", strMobName + " (Heroic)", strMobName)

                        Dim objExistingInstance As tss_InstanceInfo = tss_DKP.tss_GetInstanceByName(strInstanceName, CInt(Settings("BoardID")))

                        If (objExistingInstance Is Nothing) Then
                            strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                + "Mob (" + strMobName + ") will be added."
                        Else
                            Dim objExistingMob As tss_MobInfo = tss_DKP.tss_GetMobByName(strDKPMobName, objExistingInstance.InstanceID)

                            If (objExistingMob Is Nothing) Then
                                If (ImportType = ImportType.Preview) Then
                                    strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                        + "Mob (" + strMobName + ") will be added."
                                ElseIf (ImportType = ImportType.Confirm) Then
                                    Dim objNewMob As New tss_MobInfo : objNewMob = CBO.InitializeObject(objNewMob, GetType(tss_MobInfo))
                                    objNewMob.MobName = strDKPMobName
                                    objNewMob.MobValue = 0
                                    objNewMob.MobWowHeadID = ""
                                    objNewMob.MobHeroicFlag = IIf(strDifficulty = "Heroic", True, False)
                                    objNewMob.MobAttendanceFlag = True
                                    objNewMob.InstanceID = objExistingInstance.InstanceID
                                    Try
                                        dicMobs.Add(strMobName, tss_DKP.tss_AddMob(objNewMob))
                                    Catch ex As Exception
                                        Return "Error: Trying to add mob resulted in the following error (" + ex.Message + ")."
                                    End Try
                                End If
                            Else
                                dicMobs.Add(strMobName, objExistingMob.MobID)
                            End If

                            If (ImportType = ImportType.Confirm) Then

                                Dim objNewRaid As New tss_RaidInfo : objNewRaid = CBO.InitializeObject(objNewRaid, GetType(tss_RaidInfo))
                                objNewRaid.RaidValue = 0
                                objNewRaid.RaidDate = strRaidDate
                                objNewRaid.RaidAttendanceFlag = True
                                dicMobs.TryGetValue(strMobName, objNewRaid.MobID)
                                Try
                                    dicRaids.Add(strMobName, tss_DKP.tss_AddRaid(objNewRaid))
                                Catch ex As Exception
                                    Return "Error: Trying to add raid resulted in the following error (" + ex.Message + ")."
                                End Try

                                For Each strPlayer As String In aryRoster
                                    If (dicPlayers.ContainsKey(strPlayer)) Then
                                        Dim objNewRoster As New tss_RosterInfo : objNewRoster = CBO.InitializeObject(objNewRoster, GetType(tss_RosterInfo))
                                        dicRaids.TryGetValue(strMobName, objNewRoster.RaidID)
                                        dicPlayers.TryGetValue(strPlayer, objNewRoster.PlayerID)
                                        Try
                                            tss_DKP.tss_AddRoster(objNewRoster)
                                        Catch ex As Exception
                                            Return "Error: Trying to add roster resulted in the following error (" + ex.Message + ")."
                                        End Try
                                    End If
                                Next

                            End If

                        End If

                        intCounter = intCounter + 1

                    Loop

                ElseIf (aryRaidImport(intCounter) = "Loot:") Then

                    intCounter = intCounter + 2

                    Do Until (aryRaidImport(intCounter) = "") Or (intCounter > (aryRaidImport.Length - 1))

                        Dim aryLoot() As String = aryRaidImport(intCounter).Split(",")
                        ' Loot: Index,Name,ID,Time,Mob,Player,Cost,Note
                        Dim strItemName As String = aryLoot(1).Trim("""")
                        Dim strWowHeadItemID As String = aryLoot(2).Trim("""")
                        Dim strMobName As String = aryLoot(4).Trim("""")
                        Dim strPlayerName As String = aryLoot(5).Trim("""")

                        If (dicPlayers.ContainsKey(strPlayerName)) And Not (strMobName = "Trash mob") Then

                            Dim objExistingItem As tss_ItemInfo = tss_DKP.tss_GetItemByName(strItemName, CInt(Settings("BoardID")))

                            If (objExistingItem Is Nothing) Then
                                If (ImportType = ImportType.Preview) Then
                                    strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                        + "Item (" + strItemName + ") will be added."
                                    strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                        + "Item (" + strItemName + ") will be added as a Drop to Mob (" + strMobName + ")."
                                ElseIf (ImportType = ImportType.Confirm) Then
                                    Dim objNewItem As New tss_ItemInfo : objNewItem = CBO.InitializeObject(objNewItem, GetType(tss_ItemInfo))
                                    objNewItem.ItemName = strItemName
                                    objNewItem.ItemValue = 0
                                    objNewItem.ItemWowHeadID = strWowHeadItemID
                                    objNewItem.BoardID = CInt(Settings("BoardID"))
                                    Try
                                        dicItems.Add(strItemName, tss_DKP.tss_AddItem(objNewItem))
                                    Catch ex As Exception
                                        Return "Error: Trying to add item resulted in the following error (" + ex.Message + ")."
                                    End Try
                                End If
                            Else
                                dicItems.Add(strItemName, tss_DKP.tss_AddItem(objExistingItem))
                            End If

                            If (dicItems.ContainsKey(strItemName)) Then

                                Dim intMobID, intItemID As Integer

                                dicMobs.TryGetValue(strMobName, intMobID)
                                dicItems.TryGetValue(strItemName, intItemID)

                                Dim objExistingDrop As tss_DropInfo = tss_DKP.tss_GetDropByMobIDItemID(intMobID, intItemID)

                                If (objExistingDrop Is Nothing) Then
                                    If (ImportType = ImportType.Preview) Then
                                        strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") _
                                            + "Drop (" + strItemName + ") will be added to mob (" + strMobName + ")."
                                    ElseIf (ImportType = ImportType.Confirm) Then
                                        Dim objNewDrop As New tss_DropInfo : objNewDrop = CBO.InitializeObject(objNewDrop, GetType(tss_DropInfo))
                                        objNewDrop.MobID = intMobID
                                        objNewDrop.ItemID = intItemID
                                        Try
                                            dicDrops.Add(strMobName + "|" + strItemName, tss_DKP.tss_AddDrop(objNewDrop))
                                        Catch ex As Exception
                                            Return "Error: Trying to add item resulted in the following error (" + ex.Message + ")."
                                        End Try
                                    End If
                                Else
                                    dicDrops.Add(strMobName + "|" + strItemName, objExistingDrop.DropID)
                                End If

                                Dim objNewLoot As New tss_LootInfo : objNewLoot = CBO.InitializeObject(objNewLoot, GetType(tss_LootInfo))
                                dicRaids.TryGetValue(strMobName, objNewLoot.RaidID)
                                dicDrops.TryGetValue(strMobName + "|" + strItemName, objNewLoot.DropID)
                                dicPlayers.TryGetValue(strPlayerName, objNewLoot.PlayerID)
                                objNewLoot.LootValue = 0
                                Try
                                    tss_DKP.tss_AddLoot(objNewLoot)
                                Catch ex As Exception
                                    Return "Error: Trying to add loot resulted in the following error (" + ex.Message + ")."
                                End Try

                            End If

                        End If

                        intCounter = intCounter + 1
                    Loop

                End If

            Next

            Return strWarning

        End Function

#End Region

#Region "Private Event Handlers"

        Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdRaids.ItemDataBound

            If (e.Item.ItemType.Equals(ListItemType.Item)) Or (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                Dim lblRaidID As Label = e.Item.FindControl("lblRaidID")
                Dim btnLoots As ImageButton = e.Item.FindControl("btnLoots")
                Dim btnRosters As ImageButton = e.Item.FindControl("btnRosters")
                Dim lblRaidDate As Label = e.Item.FindControl("lblRaidDate")
                Dim lnkMobWowHeadID As HyperLink = e.Item.FindControl("lnkMobWowHeadID")
                Dim lblMobName As Label = e.Item.FindControl("lblMobName")
                Dim lblRaidValue As Label = e.Item.FindControl("lblRaidValue")

                Dim objRaid As tss_RaidInfo = e.Item.DataItem

                lblRaidID.Text = objRaid.RaidID.ToString()
                lblRaidID.Visible = False

                btnLoots.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_bags.png"
                btnLoots.CommandName = "Loots"
                btnLoots.CommandArgument = objRaid.RaidID.ToString()
                btnLoots.AlternateText = "View Loots"

                btnRosters.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_magnifier.png"
                btnRosters.CommandName = "Rosters"
                btnRosters.CommandArgument = objRaid.RaidID.ToString()
                btnRosters.AlternateText = "View Rosters"

                lblRaidDate.Text = objRaid.RaidDate.ToString("MM/dd/yy hh:mm tt")

                If (objRaid.MobWowHeadID.Length = 0) Then
                    lnkMobWowHeadID.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_blank.png"
                    lnkMobWowHeadID.Enabled = False
                Else
                    lnkMobWowHeadID.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_wowhead.png"
                    lnkMobWowHeadID.NavigateUrl = "http://www.wowhead.com/?npc=" + objRaid.MobWowHeadID
                    lnkMobWowHeadID.Target = "_new"
                    lnkMobWowHeadID.Enabled = True
                    lnkMobWowHeadID.ToolTip = "View WowHead"
                End If

                lblMobName.Text = objRaid.MobName
                lblRaidValue.Text = objRaid.RaidValue

            End If

        End Sub

        Private Sub ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles grdRaids.ItemCommand

            Dim tss_DKP As New tss_DataController
            Dim tss_DKP_Parent As tss_DKP = Me.Parent.Parent

            If (e.CommandName = "Rosters") Then

                Dim objExistingRaid As tss_RaidInfo = tss_DKP.tss_GetRaid(CInt(e.CommandArgument))
                If (objExistingRaid Is Nothing) Then
                    ManagePageState(PageState.View)
                Else
                    tss_DKP_Parent.lnkManageRaidRosters_Click(objExistingRaid)
                End If

            ElseIf (e.CommandName = "Loots") Then

                Dim objExistingRaid As tss_RaidInfo = tss_DKP.tss_GetRaid(CInt(e.CommandArgument))
                If (objExistingRaid Is Nothing) Then
                    ManagePageState(PageState.View)
                Else
                    tss_DKP_Parent.lnkManageRaidLoots_Click(objExistingRaid)
                End If

            End If

        End Sub

        Private Sub ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdRaids.ItemCreated

            If (e.Item.ItemType.Equals(ListItemType.Header)) Then

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridHeader" + grdRaids.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Item)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridItem" + grdRaids.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridAltItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridAltItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridAltItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridAltItem" + grdRaids.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Footer)) Then

                e.Item.Visible = False

            End If

        End Sub

        Private Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPageSize.SelectedIndexChanged, ddlMobID.SelectedIndexChanged

            If (sender.Equals(ddlPageSize)) Then

                vsPageSize = ddlPageSize.SelectedValue
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(ddlMobID)) Then

                BindTextBoxes()

            End If

        End Sub

        Private Sub SortCommand(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles grdRaids.SortCommand

            If (vsSortExpression = e.SortExpression) Then
                If (vsSortDirection = "ASC") Then
                    vsSortDirection = "DESC"
                ElseIf (vsSortDirection = "DESC") Then
                    vsSortDirection = "ASC"
                End If
            Else
                vsSortExpression = e.SortExpression
                vsSortDirection = "ASC"
            End If

            ManagePageState(PageState.View)

        End Sub

        Private Sub Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkFirstPage.Click, lnkPreviousPage.Click, lnkNextPage.Click, _
            lnkLastPage.Click, lnkAdd.Click, lnkUpdate.Click, lnkDelete.Click, lnkImport.Click, lnkConfirm.Click, lnkSubmit.Click, lnkCancel.Click

            Dim tss_DKP As New tss_DataController

            lblWarning.Text = Null.NullString

            If (sender.Equals(lnkFirstPage)) Then

                vsCurrentPage = 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkPreviousPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) - 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkNextPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) + 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkLastPage)) Then

                vsCurrentPage = vsTotalPages
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkAdd)) Then

                ManagePageState(PageState.Add)

            ElseIf (sender.Equals(lnkUpdate)) Then

                Dim boolIsChecked As Boolean = False

                For Each currentRaid As DataGridItem In grdRaids.Items

                    If (currentRaid.ItemType.Equals(ListItemType.Item)) Or (currentRaid.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkRaid As CheckBox = currentRaid.FindControl("chkRaid")
                        Dim lblRaidID As Label = currentRaid.FindControl("lblRaidID")

                        If (chkRaid.Checked) And (boolIsChecked) Then

                            ManagePageState(PageState.MultiUpdate)
                            Exit For

                        ElseIf (chkRaid.Checked) And Not (boolIsChecked) Then

                            boolIsChecked = True

                            ManagePageState(PageState.SingleUpdate)

                            Dim objExistingRaid As tss_RaidInfo = tss_DKP.tss_GetRaid(CInt(lblRaidID.Text))

                            If (objExistingRaid Is Nothing) Then
                                ManagePageState(PageState.View)
                                lblWarning.Text = "Error: Raid in selection not found."
                                Exit For
                            Else
                                txtRaidValue.Text = objExistingRaid.RaidValue
                                txtRaidDate.Text = objExistingRaid.RaidDate.ToString("MM/dd/yy hh:mm tt")
                                ddlMobID.SelectedValue = objExistingRaid.MobID
                                chkRaidAttendanceFlag.Checked = objExistingRaid.RaidAttendanceFlag
                            End If
                        End If

                    End If

                Next

                If Not (boolIsChecked) Then
                    ManagePageState(PageState.View)
                End If

            ElseIf (sender.Equals(lnkDelete)) Then

                For Each currentRaid As DataGridItem In grdRaids.Items

                    If (currentRaid.ItemType.Equals(ListItemType.Item)) Or (currentRaid.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkRaid As CheckBox = currentRaid.FindControl("chkRaid")
                        Dim lblRaidID As Label = currentRaid.FindControl("lblRaidID")

                        If (chkRaid.Checked) Then
                            tss_DKP.tss_DeleteRaid(CInt(lblRaidID.Text))
                        End If

                    End If

                Next

                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkImport)) Then

                ManagePageState(PageState.Import)

            ElseIf (sender.Equals(lnkSubmit)) Then

                If (vsPageState = PageState.Add) Then

                    If (txtRaidValue.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Raid value can not be empty."
                    ElseIf (txtRaidDate.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Raid date can not be empty."
                    ElseIf Not IsDate(txtRaidDate.Text) Then
                        lblWarning.Text = "Error: Raid date is not a valid date."
                    ElseIf (ddlMobID.SelectedValue.Length = 0) Then
                        lblWarning.Text = "Error: Mob name can not be empty."
                    Else

                        Dim aryRaidRoster() As String = Split(txtRaidRoster.Text.Trim, ";")

                        For Each currentPlayer As String In aryRaidRoster
                            Dim objExistingPlayer As tss_PlayerInfo = tss_DKP.tss_GetPlayerByName(currentPlayer, CInt(Settings("BoardID")))

                            If (objExistingPlayer Is Nothing) Then
                                lblWarning.Text = lblWarning.Text + IIf(lblWarning.Text.Length > 0, "<br>", "") _
                                    + "Player (" + currentPlayer + ") not found in database."
                            End If
                        Next

                        If Not (lblWarning.Text = Null.NullString) Then
                            Exit Sub
                        End If

                        Dim objNewRaid As New tss_RaidInfo : objNewRaid = CBO.InitializeObject(objNewRaid, GetType(tss_RaidInfo))

                        objNewRaid.MobID = ddlMobID.SelectedValue
                        objNewRaid.RaidValue = txtRaidValue.Text.Trim
                        objNewRaid.RaidDate = txtRaidDate.Text.Trim
                        objNewRaid.RaidAttendanceFlag = chkRaidAttendanceFlag.Checked

                        Dim intRaidID As Integer = 0

                        Try
                            intRaidID = tss_DKP.tss_AddRaid(objNewRaid)
                        Catch ex As ApplicationException
                            lblWarning.Text = "Error: Trying to add raid resulted in the following error (" + ex.Message + ")."
                        End Try

                        For Each currentPlayer As String In aryRaidRoster

                            Dim objExistingPlayer As tss_PlayerInfo = tss_DKP.tss_GetPlayerByName(currentPlayer, CInt(Settings("BoardID")))
                            Dim objNewRoster As New tss_RosterInfo : objNewRoster = CBO.InitializeObject(objNewRoster, GetType(tss_RosterInfo))

                            objNewRoster.RaidID = intRaidID
                            objNewRoster.PlayerID = objExistingPlayer.PlayerID

                            Try
                                tss_DKP.tss_AddRoster(objNewRoster)
                            Catch ex As ApplicationException
                                lblWarning.Text = lblWarning.Text + IIf(lblWarning.Text.Length > 0, "<br>", "") _
                                    + "Error: Trying to add roster for raid resulted in the following error (" + ex.Message + ")."
                            End Try
                        Next

                    End If

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.SingleUpdate) Then

                    For Each currentRaid As DataGridItem In grdRaids.Items

                        If (currentRaid.ItemType.Equals(ListItemType.Item)) Or (currentRaid.ItemType.Equals(ListItemType.AlternatingItem)) Then

                            Dim chkRaid As CheckBox = currentRaid.FindControl("chkRaid")
                            Dim lblRaidID As Label = currentRaid.FindControl("lblRaidID")

                            If (chkRaid.Checked) Then

                                Dim objExistingRaid As tss_RaidInfo = tss_DKP.tss_GetRaid(CInt(lblRaidID.Text))

                                If (objExistingRaid Is Nothing) Then
                                    lblWarning.Text = "Error: Trying to update raid resulted in the following error (Raid in selection not found.)."
                                Else

                                    objExistingRaid.MobID = ddlMobID.SelectedValue
                                    objExistingRaid.RaidValue = txtRaidValue.Text.Trim
                                    objExistingRaid.RaidDate = txtRaidDate.Text.Trim
                                    objExistingRaid.RaidAttendanceFlag = chkRaidAttendanceFlag.Checked

                                    Try
                                        tss_DKP.tss_UpdateRaid(objExistingRaid)
                                    Catch ex As ApplicationException
                                        lblWarning.Text = "Error: Trying to update player resulted in the following error (" + ex.Message + ")."
                                    End Try

                                End If

                            End If

                        End If

                    Next

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.MultiUpdate) Then

                    For Each currentRaid As DataGridItem In grdRaids.Items

                        If (currentRaid.ItemType.Equals(ListItemType.Item)) Or (currentRaid.ItemType.Equals(ListItemType.AlternatingItem)) Then

                            Dim chkRaid As CheckBox = currentRaid.FindControl("chkRaid")
                            Dim lblRaidID As Label = currentRaid.FindControl("lblRaidID")

                            If (chkRaid.Checked) Then

                                Dim objExistingRaid As tss_RaidInfo = tss_DKP.tss_GetRaid(CInt(lblRaidID.Text))

                                If (objExistingRaid Is Nothing) Then
                                    lblWarning.Text = "Error: Trying to update raid resulted in the following error (Raid in selection not found.)."
                                Else

                                    If (chkIncludeRaidValue.Checked) Then
                                        objExistingRaid.RaidValue = txtRaidValue.Text.Trim
                                    End If

                                    If (chkIncludeRaidDate.Checked) Then
                                        objExistingRaid.RaidDate = txtRaidDate.Text.Trim
                                    End If

                                    If (chkIncludeRaidAttendanceFlag.Checked) Then
                                        objExistingRaid.RaidAttendanceFlag = chkRaidAttendanceFlag.Checked
                                    End If

                                    Try
                                        tss_DKP.tss_UpdateRaid(objExistingRaid)
                                    Catch ex As ApplicationException
                                        lblWarning.Text = "Error: Trying to update raid (" + objExistingRaid.MobName _
                                            + " (" + objExistingRaid.RaidDate.ToString("MM/dd/yy hh:mm tt") _
                                            + ")) resulted in the following error (" + ex.Message + ")."
                                    End Try

                                End If

                            End If

                        End If

                    Next

                    BindPage()

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.Import) Then

                    If (txtRaidImport.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Raid import can not be empty."
                    Else
                        lblRaidConfirmWarning.Text = RaidImport(txtRaidImport.Text, ImportType.Preview)
                    End If

                    If (lblRaidConfirmWarning.Text.Length = 0) Then
                        lblWarning.Text = RaidImport(txtRaidImport.Text, ImportType.Confirm)
                    Else
                        pnlRaidConfirm.Visible = True
                    End If

                    BindPage()

                    If (lblRaidConfirmWarning.Text = Null.NullString) And (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                End If

            ElseIf (sender.Equals(lnkCancel)) Then

                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkConfirm)) Then

                If (txtRaidImport.Text.Trim.Length = 0) Then
                    lblWarning.Text = "Error: Raid import can not be empty."
                Else
                    lblWarning.Text = RaidImport(txtRaidImport.Text, ImportType.Confirm)
                End If

                BindPage()

                If (lblWarning.Text = Null.NullString) Then
                    ManagePageState(PageState.View)
                End If

            End If

        End Sub

#End Region

#Region "Public Methods"

        Public Sub Initialize()

            ManagePageState(PageState.View)

        End Sub

#End Region

    End Class

End Namespace
